home *** CD-ROM | disk | FTP | other *** search
/ Wayzata's Best of Shareware PC/Windows 1 / Wayzata's Best of Shareware for PC-Windows - Release 1 - Wayzata Technology (1993).iso / mac / DOS / TELECOMM / PCCP047 / XMODEMS.C < prev    next >
Text File  |  1992-07-10  |  3KB  |  171 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC: >qcl term.c graphics.lib
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13.  
  14. #define NAK 21
  15. #define ACK 6
  16. #define SOH 1
  17. #define EOT 4
  18. #define CAN 24
  19.  
  20. sendchar(c)
  21.     unsigned char c;
  22.     {
  23.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  24.     outp(basereg, c);
  25.     }
  26.  
  27. int follow;
  28.  
  29. int rcharto(ticks)
  30.     int ticks;
  31.     {
  32.     long tstamp, tstamp1, dayofticksp;
  33.     int c;
  34.     _bios_timeofday(_TIME_GETCLOCK, &tstamp);
  35.     dayofticksp=0;
  36.     while(1)
  37.         {
  38.         if(kbhit())
  39.             getch();
  40.         if(_bios_timeofday(_TIME_GETCLOCK, &tstamp1))
  41.             dayofticksp+=20*60*60*24;
  42.         if(tstamp1+dayofticksp-tstamp>ticks)
  43.             return(-1); /* NOTE: This is an INT!!! */
  44.         if(follow!=index)
  45.             {
  46.             c=buf[follow++];
  47.             follow=follow%TBUFSIZ;
  48.             return(c);
  49.             }
  50.         }
  51.     }
  52.  
  53.  
  54. unsigned char block[128];
  55.  
  56. sblock(blockn)
  57.     unsigned char blockn;
  58.     {
  59.     int i;
  60.     unsigned char checksum;
  61.     checksum=0;
  62.     sendchar(SOH);
  63.     sendchar(blockn);
  64.     sendchar((blockn^0xff)&0xff);
  65.     for(i=0;i<128;++i)
  66.         {
  67.         sendchar(block[i]);
  68.         checksum+=block[i];
  69.         }
  70.     sendchar(checksum);
  71.     }
  72.  
  73. quit()
  74.     {
  75.     cleanup(0);
  76.     exit(99);
  77.     }
  78.  
  79. main(argc, argv)
  80.     int argc;
  81.     char **argv;
  82.     {
  83.     int i, j, infd, ok, c;
  84.     unsigned char blocknum;
  85.     long nbytes;
  86.     index=follow=0;
  87.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  88.     printf("xmodem checksum send of %s.\n", argv[4]);
  89.     if(argc!=5)
  90.         {
  91.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  92.         exit(1);
  93.         }
  94.     if((infd=open(argv[4], O_RDONLY|O_BINARY))==-1)
  95.         {
  96.         printf("Error opening file %s.\n", argv[4]);
  97.         exit(2);
  98.         }
  99.     comnum=atoi(argv[1])-1;
  100.     speed=atoi(argv[2]);
  101.     databits='8';
  102.     parity='n';
  103.     stopbits=argv[3][0];
  104.     setport();
  105.     signal(SIGINT, quit);
  106.     readset();
  107.     setup();
  108.     nbytes=0;
  109.     if(rcharto(2000)!=NAK)
  110.         {
  111.         printf("Spurrious char or no NAK in 100 seconds.\n");
  112.         cleanup(0);
  113.         exit(10);
  114.         }
  115.     blocknum=1;
  116.     while(1)
  117.         {
  118.         if((j=read(infd, block, 128))==0)
  119.             {
  120.             printf("End of file.\n");
  121.             sendchar(EOT);
  122.             do
  123.                 c=rcharto(300);
  124.             while((c!=ACK)&&(c!=NAK)&&(c!=-1));
  125.             if(c!=ACK)
  126.                 {
  127.                 printf("\nNo ACK of EOT.\n");
  128.                 cleanup(0);
  129.                 exit(13);
  130.                 }
  131.             else
  132.                 {
  133.                 printf("\nSuccessful.\n");
  134.                 cleanup(0);
  135.                 exit(0);    
  136.                 }
  137.             }
  138.         for(c=j;c<128;c++)
  139.             block[c]=26;
  140.         i=0;
  141.         do
  142.             {
  143.             printf("\nSending block %d. ", blocknum);
  144.             sblock(blocknum);
  145.             do
  146.                 c=rcharto(200);
  147.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  148.             }
  149.         while((c==NAK)&&(i++<10));
  150.         if(c!=ACK)
  151.             if(c==NAK)
  152.                 {
  153.                 printf("\nRetry limit exceeded.\n");
  154.                 cleanup(0);
  155.                 exit(14);
  156.                 }
  157.             else
  158.                 {
  159.                 printf("\nSpurrious character hex %02x; ACK or NAK expected.\n", c);
  160.                 cleanup(0);
  161.                 exit(11);
  162.                 }
  163.         nbytes+=128;
  164.         printf("Successful. Bytes so far: %ld", nbytes);
  165.         blocknum++;
  166.         }
  167.     printf("Programming error; fell through end; see code.\n");
  168.     cleanup(0);
  169.     exit(12);
  170.     }
  171.